package com.iseastar.dianxiaosan.home.adapter;

import android.app.Activity;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.iseastar.dianxiaosan.model.ParcelOrderedBean;
import com.iseastar.guojiang.app.Intents;
import com.shouhuobao.station.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import droid.frame.activity.base.BaseAdapterWithImage;

/**
 * 用户已下单
 *
 * @author kaka<br>
 *         2017年3月28日
 */
public class ParcelOrderedAdapter extends BaseAdapterWithImage<ParcelOrderedBean> {

    private SimpleDateFormat sdf;

    public ParcelOrderedAdapter(ArrayList<ParcelOrderedBean> items, Activity context, ViewGroup viewGroup) {
        super(items, context, viewGroup);
        sdf = new SimpleDateFormat("yyyy-MM-dd");
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.parcel_ordered_item, parent, false);
            holder.code = (TextView) convertView.findViewById(R.id.parcel_wait_code);
            holder.number = (TextView) convertView.findViewById(R.id.parcel_wait_sender_phone);
            holder.time = (TextView) convertView.findViewById(R.id.parcel_wait_arrival_time);
            holder.photo = (ImageView) convertView.findViewById(R.id.parcel_wait_photo);
            holder.telePhone = (ImageView) convertView.findViewById(R.id.parcel_wait_phone);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final ParcelOrderedBean item = items.get(position);
        // set image
        showImage(item.getParcelImgUrl(), holder.photo, null);
        holder.number.setText(Html.fromHtml("<font color=#888888>电话: </font>" + item.getSenderPhone()));// 收件人电话
        if (TextUtils.isEmpty(item.getBagCode()) || "null".equals(item.getBagCode())) {
            holder.code.setText(Html.fromHtml("<font color=#888888>投递编号: </font>" + context.getString(R.string.parcel_nobag_code)));// 投递袋编码
        } else {
            holder.code.setText(Html.fromHtml("<font color=#888888>投递编号: </font>" + item.getBagCode()));// 投递袋编码
        }

        // Date date = new Date(item.getPostTime());
        // String time = sdf.format(date);
        if (TextUtils.isEmpty(item.getPostTime()) || "null".equals(item.getPostTime())) {
            holder.time.setText(Html.fromHtml("<font color=#888888>下单时间: </font>" + item.getCreateTime()));//下单时间
        } else {
            holder.time.setText(Html.fromHtml("<font color=#888888>到店时间: </font>" + item.getPostTime()));// 到店时间
        }

        holder.telePhone.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intents.dialOrCall(item.getSenderPhone());
            }
        });
        return convertView;
    }

    private static class ViewHolder {
        TextView code, number, time;
        ImageView photo, telePhone;// 包裹中寄件照片,寄件人电话
    }
}
